<template>
  <el-dropdown @command="handleMenuClick">
    <span :class="[prefixCls, `${prefixCls}--${theme}`]" class="flex">
      <img :class="`${prefixCls}__header`" :src="getUserInfo.avatar" />
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name  `" class="truncate">
          {{ getUserInfo.realName }}
        </span>
      </span>
    </span>
    <template #dropdown>
      <el-dropdown-item command="doc" v-if="getShowDoc" class="p-2 px-6">
        <Icon icon="ion:document-text-outline" class="mr-2"></Icon>
        <span>{{ t('layout.header.dropdownItemDoc') }}</span>
      </el-dropdown-item>
      <el-dropdown-item command="lock" v-if="getUseLockPage" class="p-2 px-6">
        <Icon icon="ion:lock-closed-outline" class="mr-2"></Icon>
        <span>{{ t('layout.header.tooltipLock') }}</span>
      </el-dropdown-item>
      <el-dropdown-item command="switchApp" class="p-2 px-6">
        <Icon icon="clarity:switch-line" class="mr-2"></Icon>
        <span>切换应用</span>
      </el-dropdown-item>
      <el-dropdown-item command="logout" class="p-2 px-6">
        <Icon icon="ion:power-outline" class="mr-2"></Icon>
        <span>{{ t('layout.header.dropdownItemLoginOut') }}</span>
      </el-dropdown-item>
    </template>
  </el-dropdown>
  <LockAction @register="register" />
  <AppForm ref='switchAppMod' :reload='true' :closable='true' />
</template>
<script lang="ts">
// components
import { Dropdown, Menu } from 'ant-design-vue';

import { defineComponent, computed, ref } from 'vue';

import { DOC_URL } from '/@/settings/siteSetting';

import { useUserStore } from '/@/store/modules/user';
import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
import { useI18n } from '/@/hooks/web/useI18n';
import { useDesign } from '/@/hooks/web/useDesign';
import { useModal } from '/@/components/Modal';

import headerImg from '/@/assets/images/header.jpg';
import { propTypes } from '/@/utils/propTypes';
import { openWindow } from '/@/utils';

import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
import AppForm from '/@/views/sys/login/AppForm.vue';
import { Icon } from '/@/components/Icon';

type MenuEvent = 'logout' | 'doc' | 'lock' | 'switchApp';

export default defineComponent({
  name: 'UserDropdown',
  components: {
    Dropdown,
    Menu,
    MenuItem: createAsyncComponent(() => import('./DropMenuItem.vue')),
    MenuDivider: Menu.Divider,
    LockAction: createAsyncComponent(() => import('../lock/LockModal.vue')),
    AppForm,
    Icon
  },
  props: {
    theme: propTypes.oneOf(['dark', 'light']),
  },
  setup() {
    const { prefixCls } = useDesign('header-user-dropdown');
    const { t } = useI18n();
    const { getShowDoc, getUseLockPage } = useHeaderSetting();
    const userStore = useUserStore();

    const getUserInfo = computed(() => {
      const { realName = '', avatar, desc } = userStore.getUserInfo || {};
      return { realName, avatar: avatar || headerImg, desc };
    });

    const [register, { openModal }] = useModal();

    function handleLock() {
      openModal(true);
    }

    //  login out
    function handleLoginOut() {
      userStore.confirmLoginOut();
    }

    // open doc
    function openDoc() {
      openWindow(DOC_URL);
    }

    function handleMenuClick(e: MenuEvent) {
      switch (e) {
        case 'logout':
          handleLoginOut();
          break;
        case 'doc':
          openDoc();
          break;
        case 'lock':
          handleLock();
          break;
        case 'switchApp':
          handleSwitchApp();
          break;
      }
    }

    const switchAppMod = ref({ visible: false })
    function handleSwitchApp() {
      switchAppMod.value.visible = true;
    }
    return {
      prefixCls,
      t,
      getUserInfo,
      handleMenuClick,
      getShowDoc,
      register,
      getUseLockPage,
      switchAppMod,
    };
  },
});
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-header-user-dropdown';

.@{prefix-cls} {
  height: @header-height;
  padding: 0 0 0 10px;
  padding-right: 10px;
  overflow: hidden;
  font-size: 12px;
  cursor: pointer;
  align-items: center;

  img {
    width: 24px;
    height: 24px;
    margin-right: 12px;
  }

  &__header {
    border-radius: 50%;
  }

  &__name {
    font-size: 14px;
  }

  &--dark {
    &:hover {
      background-color: @header-dark-bg-hover-color;
    }
  }

  &--light {
    &:hover {
      background-color: @header-light-bg-hover-color;
    }

    .@{prefix-cls}__name {
      color: @text-color-base;
    }

    .@{prefix-cls}__desc {
      color: @header-light-desc-color;
    }
  }
}
</style>
